﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebApplication1.Classes;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections.Generic;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        private User u1;
        private string Сon = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonClickAuthorization(object sender, EventArgs e)
        {
            Panel1.Visible = true;
        }
        protected void ButtonClickInput(object sender, EventArgs e)
        {

            SqlConnection connection = new SqlConnection(Сon);
           
            try
            { 
                connection.Open();
                List<User> userList = new List<User>();
                SqlCommand сommand2 = new SqlCommand("select * from User1 where Login='" + TextBoxLogin.Text +"' and Password='" + TextBoxRassword.Text +  "'", connection);
                SqlDataReader reader = сommand2.ExecuteReader();

                while (reader.Read())
                {
                    userList.Add(new User(Convert.ToInt32(reader["Id"]), Convert.ToString(reader["Password"]), Convert.ToString(reader["Role"]), Convert.ToString(reader["Name"]), Convert.ToString(reader["Surname"]), Convert.ToString(reader["Phone"]), Convert.ToString(reader["Email"]), Convert.ToString(reader["Login"])));
                }

                reader.Close();

                if (userList.Count() == 1)
                {
                    u1 = userList.First();
                    Session["id"] = u1.Id;
                    Session["name"] = u1.Name;
                    Session["surname"] = u1.Surname;
                    Session["role"] = u1.Role;
                  
                }
                else
                {
                    ButtonAuthorization.Visible = false;
                    ButtonInfo.Visible = false;
                    Label4.Visible = true;
                }
                connection.Close();
            }
            catch (SqlException ex)
            {
                connection.Close();
                Server.Transfer("Error.aspx?error=" + ex.Message);
            }
            
            Server.Transfer("Main.aspx");
            connection.Close();
            
        }

        protected void ButtonClickExit(object sender, EventArgs e)
        {
            Label5.Visible = true;
            Label6.Visible = true;
            Label2.Visible = true;
            TextBoxLogin.Visible = true;
            TextBoxLogin.Text = "";
            TextBoxRassword.Visible = true;
            TextBoxRassword.Text = "";
            ButtonInput.Visible = true;
            ButtonAuthorization.Visible = false;
            ButtonInfo.Visible = false;
            ButtonExit.Visible = false;
           
        }

        protected void ButtonClickInfo(object sender, EventArgs e)
        {
            Session["id"] = 1;
            Session["name"] = "";
            Session["surname"] = "Anonimous";
            Session["role"] = "Пользователь";
            Server.Transfer("Main.aspx");
        }
    }
}
